package game;

import java.util.Random;
import java.util.Scanner;

public class MonsterGame {

    static class Monster {
        int level = 1;
        int hp;
        int attack;
        int defense;  // 防御力
        int intelligence;  // 智力
        int stamina;  // 体力
        int dodge;  // 躲避
        String name;
        int gold; // 金币

        String abilityName; // 怪物技能名称
        int abilityCooldown; // 技能冷却回合数


        Monster(int hp, int attack, int defense, int intelligence, int stamina, int dodge, String name, int gold, String abilityName, int abilityCooldown) {
            this.hp = hp;
            this.attack = attack;
            this.defense = defense;
            this.intelligence = intelligence;
            this.stamina = stamina;
            this.dodge = dodge;
            this.name = name;
            this.gold = gold;
            this.abilityName = abilityName;
            this.abilityCooldown = abilityCooldown;
        }

        public void useAbility(Player player) {
            if (abilityCooldown <= 0) {
                // 对玩家应用技能效果
                if (abilityName.equals("毒素")) {
                    player.hp -= 35; // 例如：毒素每回合造成 35 点伤害
                    System.out.println(this.name + " 使用了毒素！你受到 35 点伤害。");
                } else if (abilityName.equals("火焰吐息")) {
                    player.hp -= this.intelligence * 2; // 例如：火焰吐息造成 2 倍智力伤害
                    System.out.println(this.name + " 使用了火焰吐息！你受到 " + (this.intelligence * 2) + " 点伤害。");
                } else if (abilityName.equals("石化")) {
                    // 例如：石化使玩家的攻击力降低 50%
                    player.originalAttack = player.attack; // 记录石化后的攻击力
                    player.attack /= 2;
                    player.isStone = true;  // 设置石化状态
                    System.out.println(this.name + " 使用了石化！你的攻击力降低 50%。");
                } else if (abilityName.equals("治疗")) {
                    // 例如：治疗使怪物恢复一定生命值
                    this.hp += this.stamina * 0.5;
                    System.out.println(this.name + " 使用了治疗！恢复了 " + (this.stamina * 0.5) + " 点生命值。");
                } else if (abilityName.equals("狂暴")) {
                    // 例如：狂暴使怪物的攻击力提高 30%
                    this.attack *= 1.3;
                    System.out.println(this.name + " 使用了狂暴！攻击力提高了 30%。");
                } else if (abilityName.equals("护盾")) {
                    // 例如：护盾使怪物的防御力提高 50%
                    this.defense *= 1.5;
                    System.out.println(this.name + " 使用了护盾！防御力提高了 50%。");
                } else if (abilityName.equals("召唤")) {
                    // 例如：召唤生成一个新的怪物
                    // 可以在这里创建一个新的怪物对象
                    // 并将其添加到游戏场景中
                    System.out.println(this.name + " 使用了召唤！生成了一个新的怪物。");
                }
                // ... 添加其他技能 ...
                abilityCooldown = 3; // 重置冷却回合数
            } else {
                abilityCooldown--;
            }
        }


        // 攻击方法
        // 在 Monster 类中，修改 attack 方法
        public void attack(Player player) {
            Random random = new Random();
            if (random.nextInt(100) < player.dodge) {
                System.out.println("你躲避了 " + this.name + " 的攻击！");
            } else {
                // 暴击判定
                if (random.nextInt(100) < this.dodge * 1.2) { //  暴击率
                    int damage = (this.attack - player.defense) * 2; // 暴击伤害翻倍
                    player.hp -= damage;
                    System.out.println(this.name + " 发动了暴击！攻击了你，造成了 " + damage + " 点伤害！");
                } else {
                    int damage = this.attack - player.defense;
                    if (damage > 0) {
                        player.hp -= damage;
                        System.out.println(this.name + " 攻击了你，造成了 " + damage + " 点伤害！");
                    } else {
                        System.out.println(this.name + " 的攻击被你防御住了！");
                    }
                }
            }
        }
    }
    static class Equipment {
            String name; // 物品名称
            String type; // 物品类型（武器或护甲）
            int attackBonus; // 攻击力加成
            int defenseBonus; // 防御力加成
            int price; // 价格

            Equipment(String name, String type, int attackBonus, int defenseBonus, int price) {
                this.name = name;
                this.type = type;
                this.attackBonus = attackBonus;
                this.defenseBonus = defenseBonus;
                this.price = price;
            }
        }

        static class Item {
            String name;
            String type; // "治疗", "攻击", "防御", "体力", "闪避", "智力"
            int effect; // 效果值（治疗量、攻击加成等）
            int price;

            Item(String name, String type, int effect, int price) {
                this.name = name;
                this.type = type;
                this.effect = effect;
                this.price = price;
            }
        }



        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Random random = new Random();
            // 创建玩家
            Player player = new Player(100, 35, 5, 30, 100, 80);  // 设置玩家属性

            while (player.hp > 0) { // 当玩家还有生命值时，继续战斗

                Monster monster = createMonster(random); // 创建怪物

                System.out.println("你遇到了一个 " + monster.name + "！");

                while (player.hp > 0 && monster.hp > 0) {
                    // 玩家回合
                    System.out.println("你的 HP：" + player.hp + "，" + monster.name + " 的 HP：" + monster.hp);
                    System.out.println("请选择你的行动：1. 攻击 2. 使用技能 3. 使用道具 4. 查看状态 5. 商店 6. 学习技能");
                    int choice = scanner.nextInt();

                    if (choice == 1) {
                        // 玩家攻击
                        player.attack(monster);
                    } else if (choice == 2) {
                        // 玩家使用技能
                        useSkill(player, monster, scanner);

                    } else if (choice == 3) {
                        // 玩家使用道具
                        useItem(player, scanner);

                    } else if (choice == 4) {
                        // 查看状态
                        displayPlayerStatus(player);

                    } else if (choice == 5) {

                        // 进入商店
                        enterShop(player, scanner);

                    } else if (choice == 6) {
                        // 学习技能
                        System.out.println("请选择要学习的技能：1. 火焰魔法 2. 治疗魔法 3. 吸血术");
                        int skillChoice = scanner.nextInt();
                        player.learnSkill(skillChoice);
                    } else {
                        System.out.println("无效的选项！");
                    }

                    // 怪物回合
                    if (monster.hp > 0) {
                        // 躲避判定
                        monster.attack(player);
                        monster.useAbility(player); // 怪物使用技能

                    }
                }

                handleBattleResult(player, monster); // 处理战斗结果

            }
            System.out.println("游戏结束！");
            scanner.close();
        }

        // 创建怪物的方法
        public static Monster createMonster(Random random) {
            int monsterType = random.nextInt(6);
            switch (monsterType) {
                case 0:
                    return new Monster(50, 10, 3, 5, 15, 5, "史莱姆", 10, "治疗", 5); // 史莱姆可以使用治疗
                case 1:
                    return new Monster(70, 15, 7, 8, 20, 10, "哥布林", 20, "狂暴", 3); // 哥布林可以使用狂暴
                case 2:
                    return new Monster(100, 20, 10, 5, 25, 15, "狼人", 30, "护盾", 5); // 狼人可以使用护盾
                case 3:
                    return new Monster(150, 20, 15, 10, 30, 50, "毒蛇", 40, "毒素", 5); // 新的怪物
                case 4:
                    return new Monster(180, 18, 13, 12, 22, 68, "飞行怪物", 75, "火焰吐息", 3); // 飞行类怪物
                case 5:
                    return new Monster(120, 25, 8, 15, 28, 42, "魔法怪物", 35, "石化", 5); // 魔法类怪物

                default:
                    return null; // 应该添加异常处理
            }
        }

        // 处理战斗结果的方法
        public static void handleBattleResult(Player player, Monster monster) {

            Random random = new Random();


            if (monster.hp <= 0) {
                System.out.println("你战胜了 " + monster.name + "！ ");
                player.gainExp(monster.level * 10);
                player.gold += monster.gold;
                player.skillPoints += 5;

                // 在这里还原玩家的攻击力
                if (player.isStone) {
                    player.attack = player.originalAttack;  // 恢复到石化前的攻击力
                    System.out.println("石化效果解除！你的攻击力恢复正常。");
                    player.isStone = false; // 解除石化状态
                }


                if (random.nextDouble() < 0.5) { // 50% 概率掉落物品
                    player.addToInventory(new Item("治疗药水", "healing", 50, 10));
                }

            } else {
                System.out.println("你被 " + monster.name + " 打败了！");
            }
        }

        // 进入商店的方法
        public static void enterShop(Player player, Scanner scanner) {
            System.out.println("欢迎来到商店！当前金币：" + player.gold);
            // 定义商店里的物品，但先不打印出来
            Item healingItem = new Item("治疗药水", "healing", 50, 10);
            Item attackItem = new Item("攻击药水", "attack", 15, 20);
            Item defenseItem = new Item("防御药水", "防御", 10, 15); // 防御药水
            Item staminaItem = new Item("体力药水", "体力", 20, 15); // 体力药水
            Item dodgeItem = new Item("闪避药水", "闪避", 5, 10);  // 闪避药水
            Item intelligenceItem = new Item("智力药水", "智力", 5, 10); // 智力药水
            Equipment woodSword = new Equipment("木剑", "Weapon", 10, 0, 10);
            Equipment leatherArmor = new Equipment("皮甲", "Armor", 0, 5, 10);
            Equipment ironSword = new Equipment("铁剑", "Weapon", 20, 0, 30);
            Equipment chainmail = new Equipment("锁子甲", "Armor", 0, 10, 50);
            Equipment dagger = new Equipment("匕首", "Weapon", 15, 0, 20);
            Equipment leatherBoots = new Equipment("皮靴", "Armor", 0, 3, 15);
            // 以清晰的格式展示商店里的物品
            System.out.println("1. " + woodSword.name + " (" + woodSword.price + " 金币)");
            System.out.println("2. " + leatherArmor.name + " (" + leatherArmor.price + " 金币)");
            System.out.println("3. " + ironSword.name + " (" + ironSword.price + " 金币)");
            System.out.println("4. " + chainmail.name + " (" + chainmail.price + " 金币)");
            System.out.println("5. " + dagger.name + " (" + dagger.price + " 金币)");
            System.out.println("6. " + leatherBoots.name + " (" + leatherBoots.price + " 金币)");
            System.out.println("7. " + healingItem.name + " (" + healingItem.price + " 金币)");
            System.out.println("8. " + attackItem.name + " (" + attackItem.price + " 金币)");
            System.out.println("9. " + defenseItem.name + " (" + defenseItem.price + " 金币)"); // 防御药水
            System.out.println("10. " + staminaItem.name + " (" + staminaItem.price + " 金币)"); // 体力药水
            System.out.println("11. " + dodgeItem.name + " (" + dodgeItem.price + " 金币)"); // 闪避药水
            System.out.println("12. " + intelligenceItem.name + " (" + intelligenceItem.price + " 金币)"); // 智力药水
            System.out.println("请选择你想要购买的物品：");
            int shopChoice = scanner.nextInt();


            if (shopChoice == 1) {
                // 木剑
                if (player.gold >= woodSword.price) {
                    player.gold -= woodSword.price;
                    player.equip(woodSword);
                } else {
                    System.out.println("你的金币不足，无法购买" + woodSword.name + "！");
                }
            } else if (shopChoice == 2) {
                // 皮甲
                if (player.gold >= leatherArmor.price) {
                    player.gold -= leatherArmor.price;
                    player.equip(leatherArmor);
                } else {
                    System.out.println("你的金币不足，无法购买" + leatherArmor.name + "！");
                }
            } else if (shopChoice == 3) {
                // 铁剑
                if (player.gold >= ironSword.price) {
                    player.gold -= ironSword.price;
                    player.equip(ironSword);
                } else {
                    System.out.println("你的金币不足，无法购买" + ironSword.name + "！");
                }
            } else if (shopChoice == 4) {
                // 锁子甲
                if (player.gold >= chainmail.price) {
                    player.gold -= chainmail.price;
                    player.equip(chainmail);
                } else {
                    System.out.println("你的金币不足，无法购买" + chainmail.name + "！");
                }
            } else if (shopChoice == 5) {
                // 匕首
                if (player.gold >= dagger.price) {
                    player.gold -= dagger.price;
                    player.equip(dagger);
                } else {
                    System.out.println("你的金币不足，无法购买" + dagger.name + "！");
                }
            } else if (shopChoice == 6) {
                // 皮靴
                if (player.gold >= leatherBoots.price) {
                    player.gold -= leatherBoots.price;
                    player.equip(leatherBoots);
                } else {
                    System.out.println("你的金币不足，无法购买" + leatherBoots.name + "！");
                }
            } else if (shopChoice == 7) {
                // 治疗药水
                if (player.gold >= healingItem.price) {
                    player.gold -= healingItem.price;
                    player.addToInventory(healingItem);
                } else {
                    System.out.println("你的金币不足，无法购买" + healingItem.name + "！");
                }
            } else if (shopChoice == 8) {
                // 攻击药水
                if (player.gold >= attackItem.price) {
                    player.gold -= attackItem.price;
                    player.addToInventory(attackItem);
                } else {
                    System.out.println("你的金币不足，无法购买" + attackItem.name + "！");
                }
            } else if (shopChoice == 9) {
                // 防御药水
                if (player.gold >= defenseItem.price) {
                    player.gold -= defenseItem.price;
                    player.addToInventory(defenseItem);
                } else {
                    System.out.println("你的金币不足，无法购买" + defenseItem.name + "！");
                }
            } else if (shopChoice == 10) {
                // 体力药水
                if (player.gold >= staminaItem.price) {
                    player.gold -= staminaItem.price;
                    player.addToInventory(staminaItem);
                } else {
                    System.out.println("你的金币不足，无法购买" + staminaItem.name + "！");
                }
            } else if (shopChoice == 11) {
                // 闪避药水
                if (player.gold >= dodgeItem.price) {
                    player.gold -= dodgeItem.price;
                    player.addToInventory(dodgeItem);
                } else {
                    System.out.println("你的金币不足，无法购买" + dodgeItem.name + "！");
                }
            } else if (shopChoice == 12) {
                // 智力药水
                if (player.gold >= intelligenceItem.price) {
                    player.gold -= intelligenceItem.price;
                    player.addToInventory(intelligenceItem);
                } else {
                    System.out.println("你的金币不足，无法购买" + intelligenceItem.name + "！");
                }
            } else {
                System.out.println("无效的商店选择！");
            }
        }

        // 使用道具的方法
        public static void useItem(Player player, Scanner scanner) {
            player.displayInventory(); // 显示玩家的背包
            System.out.println("请选择你的道具：");
            for (int i = 0; i < player.inventory.size(); i++) {
                System.out.println((i + 1) + ". " + player.inventory.get(i).name);
            }
            // ... (从库存显示药水) ...
            int itemChoice = scanner.nextInt();
            // 遍历背包，找到用户选择的物品
            Item Item = null;
            for (int i = 0; i < player.inventory.size(); i++) {
                if (i + 1 == itemChoice) { // 检查用户选择的物品是否与当前索引 + 1 匹配
                    Item = player.inventory.get(i);
                    break; // 找到物品后退出循环
                }
            }
            if (Item == null) { // 处理无效道具选择
                System.out.println("无效的道具选择！");
                return; // 退出方法
            }
            // 根据 Item.type 判断道具类型并执行相应操作
            if (Item.type.equals("healing")) { // 治疗药水
                player.hp += Item.effect;
                System.out.println("你使用了 " + Item.name + "，恢复了 " + Item.effect + " 点生命值！");
            } else if (Item.type.equals("attack")) { // 攻击药水
                player.attack += Item.effect;
                System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点攻击力！");
            } else if (Item.type.equals("防御")) { // 防御药水
                player.defense += Item.effect;
                System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点防御力！");
            } else if (Item.type.equals("体力")) { // 体力药水
                player.stamina += Item.effect;
                System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点体力！");
            } else if (Item.type.equals("闪避")) { // 闪避药水
                player.dodge += Item.effect;
                System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点闪避！");
            } else if (Item.type.equals("智力")) { // 智力药水
                player.intelligence += Item.effect;
                System.out.println("你使用了 " + Item.name + "，提升了 " + Item.effect + " 点智力！");
            } else {
                System.out.println("未知的道具类型！"); // 可以添加其他道具类型的判断
            }
            player.removeFromInventory(Item); // 使用后从背包移除
        }

        // 查看状态的方法
        public static void displayPlayerStatus(Player player) {
            System.out.println("你的状态：");
            System.out.println("HP：" + player.hp);
            System.out.println("攻击力：" + player.attack);
            System.out.println("防御力：" + player.defense);
            System.out.println("智力：" + player.intelligence);
            System.out.println("体力：" + player.stamina);
            System.out.println("躲避：" + player.dodge);
            System.out.println("等级：" + player.level);
            System.out.println("经验值：" + player.exp);
            System.out.println("技能点：" + player.skillPoints);
            System.out.println("金币：" + player.gold);
        }

        // 使用技能的方法
        public static void useSkill(Player player, Monster monster, Scanner scanner) {
            System.out.println("请选择你的技能：1. 治疗 2. 火焰魔法 3. 吸血术");
            int skillChoice = scanner.nextInt();
            if (skillChoice == 1) {
                player.useHealMagic();
            } else if (skillChoice == 2) {
                player.useFireMagic(monster);
            } else if (skillChoice == 3) {
                // 使用吸血术，直接调用 attack 方法
                player.attack(monster); //  在攻击的同时吸血
                System.out.println("你使用了吸血术！"); // 打印提示
            } else {
                System.out.println("无效的技能选择！");
            }
        }


}